package cn.edu.cqut.mdm.core.invoker;

import java.util.Arrays;

import cn.edu.cqut.mdm.core.message.RequestMessage;
import cn.edu.cqut.mdm.core.message.ResponseMessage;
import cn.edu.cqut.mdm.core.module.RemoteMethod;
import cn.edu.cqut.mdm.core.module.RemoteModuleEntity;

public class Invoker {
	private RemoteModuleEntity remoteModule;
	
	public Invoker(RemoteModuleEntity remoteModule) {
		this.remoteModule = remoteModule;
	}
	
	public Object invoke(RemoteMethod method, Object... params) {
		RequestMessage requestMessage = new RequestMessage();
		requestMessage.setRemoteModuleUid(remoteModule.getUid());
		requestMessage.setParameters(Arrays.asList(params));
		requestMessage.setMethod(method);
		requestMessage.setUrl(remoteModule.getUrl());
		ResponseMessage responseMessage = remoteModule.getPublisherDispatcher().getDispatcher().dispatch(requestMessage);
		// TODO 错误处理
		return responseMessage.getReturnValue();
	}
	
}
